﻿Imports System.Windows.Controls
Imports System.Linq
Imports System.Windows
Imports System

Partial Public Class Content
    Inherits Page

    Public Enum PageStatuses
        Adding
        Updating
        Removing
        Viewing
    End Enum


    Private _pageStatus As PageStatuses = PageStatuses.Viewing


    Public Sub New()
        InitializeComponent()
    End Sub

    Private Shared Sub RefreshNavigationMenu()

        DirectCast(App.Current, App).Main.RefreshNavigationMenu()

    End Sub

    Private Sub UpdatePageMode()

        If WebContext.Current.User.IsInRole("Administrators") Then

            VisualStateManager.GoToState(Me, "Editable", True)

        Else

            VisualStateManager.GoToState(Me, "ReadOnly", True)

        End If

    End Sub

    Private Sub UserStatusChanged(ByVal sender As Object, ByVal e As System.Windows.Ria.ApplicationServices.AuthenticationEventArgs)


        UpdatePageMode()

    End Sub



    'Executes when the user navigates to this page.
    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)

        If Me.NavigationContext.QueryString.ContainsKey("title") Then

            Me.Title = Me.NavigationContext.QueryString("title")

            Me.LayoutRoot.DataContext = Me.Title

        End If

        Me.UpdatePageMode()

        AddHandler WebContext.Current.Authentication.LoggedIn, AddressOf UserStatusChanged

        AddHandler WebContext.Current.Authentication.LoggedOut, AddressOf UserStatusChanged



    End Sub

    Private Sub SiteDomainService_LoadedData(ByVal sender As Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles SiteDomainService.LoadedData

        Dim currentPage As Data.Page = (From p In Me.SiteDomainService.Data).Cast(Of Data.Page).FirstOrDefault()

        Me.Title = currentPage.Title

        Me.DataContext = currentPage

        Me.ContentPanel.DataContext = Me.DataContext

        'Me.EditPanel.DataContext = Me.DataContext

        Me.transitioningContentControl.Content = Me.DataContext


    End Sub



    Private Sub ControlPanelImage_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles ControlPanelImage.MouseLeftButtonUp

        If Me.ControlPanelBorder.Opacity = 1 Then

            VisualStateManager.GoToState(Me, "Editable", True)

        Else

            VisualStateManager.GoToState(Me, "ControlPanelVisible", True)

        End If

    End Sub

    Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles SaveButton.Click

        DirectCast(Me.DataContext, Data.Page).Html = Me.HtmlEditor.Html

        If Me.SiteDomainService.IsBusy = False Then

            Me.SiteDomainService.SubmitChanges()

            VisualStateManager.GoToState(Me, "ControlPanelVisible", True)
            
        End If


    End Sub

    Private Sub EditPage_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles EditPage.MouseLeftButtonUp

        Me._pageStatus = PageStatuses.Updating

        VisualStateManager.GoToState(Me, "Editing", True)

        Me.HtmlEditor.Html = DirectCast(Me.DataContext, Data.Page).Html

    End Sub

    Private Sub AddPage_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles AddPage.MouseLeftButtonUp

        Me._pageStatus = PageStatuses.Adding

        Dim newPage As Data.Page = New Data.Page()

        With newPage
            .ChangeFrequency = ""

            .Description = ""

            .Html = ""

            .Keywords = ""

            .Priority = 0

        End With

        Dim list As System.Windows.Controls.Ria.DomainDataSourceView = _
            DirectCast(Me.SiteDomainService.Data, System.Windows.Controls.Ria.DomainDataSourceView)

        list.Add(newPage)


        Me.DataContext = newPage

        Me.EditPanel.DataContext = newPage


        Me.HtmlEditor.Html = newPage.Html

        VisualStateManager.GoToState(Me, "Editing", True)


    End Sub

    Private Sub DeletePage_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles DeletePage.MouseLeftButtonUp

        Dim confirmWin As New ConfirmationWindow

        AddHandler confirmWin.Closed, AddressOf DeletePage_Confirmed

        confirmWin.Show()

    End Sub

    Private Sub DeletePage_Confirmed(ByVal sender As Object, ByVal e As EventArgs)

        Dim result As Nullable(Of Boolean) = DirectCast(sender, ChildWindow).DialogResult

        If result.HasValue AndAlso result = True Then

            Me._pageStatus = PageStatuses.Removing

            Dim currentPage = DirectCast(Me.DataContext, Data.Page)

            Dim list As System.Windows.Controls.Ria.DomainDataSourceView = _
              DirectCast(Me.SiteDomainService.Data, System.Windows.Controls.Ria.DomainDataSourceView)

            list.Remove(currentPage)

            Me.SiteDomainService.SubmitChanges()

        End If

    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CancelButton.Click

        Me._pageStatus = PageStatuses.Viewing

        VisualStateManager.GoToState(Me, "ControlPanelVisible", True)

    End Sub

    Private Sub RichTextBlock_LinkClicked(ByVal sender As Object, ByVal e As Liquid.RichTextBoxEventArgs)

        Browser.HtmlPage.Window.Navigate(New Uri(e.Parameter.ToString(), UriKind.RelativeOrAbsolute))

    End Sub

    Private Sub SiteDomainService_SubmittedChanges(ByVal sender As Object, ByVal e As System.Windows.Controls.SubmittedChangesEventArgs) Handles SiteDomainService.SubmittedChanges

        If e.HasError = False Then

            Select Case Me._pageStatus

                Case PageStatuses.Adding

                    RefreshNavigationMenu()

                    Me.NavigationService.Navigate(New Uri("/Page/" & Me.TitleTextBox.Text, UriKind.Relative))

                Case PageStatuses.Removing

                    RefreshNavigationMenu()

                    Me.NavigationService.Navigate(New Uri("/Page/", UriKind.Relative))

                Case PageStatuses.Updating

                Case Else

            End Select

            Me._pageStatus = PageStatuses.Viewing

        Else

            ErrorWindow.CreateNew(e.Error)

        End If
    End Sub

End Class
